/*
 * Task 2. Arrays
 */

import java.util.Scanner;
/**
 * Class finds two neighbour numbers in an array with the smallest distance to
 * each other and output the index of the first number to the screen
 * 
 * @version 0.2 24 Jan 2013
 * @author Tymur Lysak
 */
public class NeighbouringNumbers {
	private int[] inputtedArray;
	private String splitter;
	
	public static void main(String[] args) {
		NeighbouringNumbers program = new NeighbouringNumbers();
		program.setSplitter(" ");
		program.getArrayFromKeyboard("Please enter the number array with \""+program.getSplitter()+ "\" splitter ");
		int result = program.getTheNeighboursIndex();
		program.outputResultToPromt(result);
	}
	
	public void getArrayFromKeyboard(String messageToUser) {
		System.out.println(messageToUser);
		String[] inputString = new Scanner(System.in).nextLine().split(getSplitter());
		int[] integerTmp = new int[inputString.length];
		for(int i = 0; i < inputString.length; i++) {
			integerTmp[i] = Integer.parseInt(inputString[i]);
		}
		inputtedArray = integerTmp;
	}
	
	public int getTheNeighboursIndex() {
		int min = Math.abs(inputtedArray[0]) + Math.abs(inputtedArray[1]);
		int index = 0;
		int sum = 0;
		for(int i = 0; i <= inputtedArray.length - 2; i++) {
			sum = Math.abs(inputtedArray[i]) + Math.abs(inputtedArray[i+1]);
			if( sum < min ){
				min = sum;
				index = i;
			}
		}
		return index;
	}
	
	public void outputResultToPromt(int resultIndex){
		System.out.print("The index of neighbouring numbers in an array with the smallest distance to each other is:");
		System.out.println(resultIndex);
		System.out.print("The first neighbour is: ");
		System.out.println(inputtedArray[resultIndex]);
		System.out.print("The second neighbour is: ");
		System.out.println(inputtedArray[resultIndex + 1]);
	}
	
	public String getSplitter() {
		return splitter;
	}

	public void setSplitter(String splitter) {
		this.splitter = splitter;
	}
}
